
package linuxbox.bbs.servlet;

import javax.servlet.http.*;

import linuxbox.BoxStatic;
import linuxbox.bbs.logic.*;
import linuxbox.common.*;

public class BbsContentAction extends Action {
	protected static String successJSP = null;

    public void doAction(HttpSession aSession, BoxSession aBoxSession, 
    HttpServletRequest req, HttpServletResponse res) throws Exception {
		BoxStatic.logger.debug("Beginning of BbsContent ... ");

		String sCatID = req.getParameter("CAT_ID");
		int iCatID = 1;
		if (sCatID != null && sCatID.length() > 0)
			iCatID = Integer.parseInt(sCatID);	
			
		BbsManager bm = BbsManager.getInstance();
		String sCatName = bm.getCatName(iCatID);
	
		long lTopicCount = bm.getTopicCount(iCatID);
		
		if (lTopicCount != 0) { 
			//long lMaxTopicID = bm.getMaxTopicID(iCatID);
			long lPages = bm.getPageCount(BbsManager.PAGE_SIZE, iCatID);

			String sPageNo = req.getParameter("PAGE_NO");
			int iPageNo;
			if (sPageNo != null && !sPageNo.equals(""))
				iPageNo = Integer.parseInt(sPageNo);
			else
				iPageNo = 1;		
			
			int iCurrentSize = BbsManager.PAGE_SIZE;
			if (lTopicCount < (long)(iPageNo * BbsManager.PAGE_SIZE))
				iCurrentSize = (int)(lTopicCount - (long)((iPageNo - 1) * BbsManager.PAGE_SIZE));
			
			req.setAttribute("CAT_ID", sCatID);			
			req.setAttribute("CAT_NAME", sCatName);			
			req.setAttribute("PAGE_SIZE", Integer.toString(iCurrentSize));			
			req.setAttribute("TOPIC_COUNT", Long.toString(lTopicCount));			
			req.setAttribute("PAGES", Long.toString(lPages));			
			req.setAttribute("PAGE_NO", Integer.toString(iPageNo));			
			
			if (iPageNo > 1)
				req.setAttribute("PREV", "true");
			else
				req.setAttribute("PREV", null);
							
			if ((long)iPageNo < lPages) 
				req.setAttribute("NEXT", "true");
			else
				req.setAttribute("NEXT", null);			
		
			BoxStatic.logger.debug("TOPIC_COUNT = " + lTopicCount);
			BoxStatic.logger.debug("PAGE_SIZE = " + iCurrentSize);			
			BoxStatic.logger.debug("PAGES = " + lPages);			
			BoxStatic.logger.debug("PAGE_NO = " + iPageNo);
		} else {
			req.setAttribute("TOPIC_COUNT", "0");
			req.setAttribute("CAT_ID", sCatID);			
			req.setAttribute("CAT_NAME", sCatName);			
			req.setAttribute("PAGE_SIZE", "0");			
			req.setAttribute("PAGES", "0");			
			req.setAttribute("PAGE_NO", "0");			
			req.setAttribute("PREV", null);
			req.setAttribute("NEXT", null);			
		}
			
		sendFile(successJSP, req, res);
	}

	public void setSuccessJSP(String str) {
		successJSP = str;
	}
}
